<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic","dojo/json", "dojo/_base/lang", "dijit/Dialog" ],
            function(array, xhr, request, topic, JSON, lang, Dialog) {
                var deletItemsCount = 0;
                carapplyRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    document.getElementById("CarApplydateNew").value="";
                    document.getElementById("CarApplydateUpdate").value="";
                    document.getElementById("carapply_starttime").value="";
                    document.getElementById("carapply_endtime").value="";
                    document.getElementById("carapply_starttimeu").value="";
                    document.getElementById("carapply_endtimeu").value="";
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#carapplyGird").jqGrid('getGridParam', 'selrow');
                        var carapply = jQuery("#carapplyGird").jqGrid('getRowData', rowid);
                        dijit.byId('carapplyUFD').setValues(carapply);
                    }
                };
                topic.subscribe("carapply/insertOrUpdate", carapplyRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(function(response) {
                        topic.publish("notifyMsgTopic", {
                            message : response,
                            type : "message",
                            duration : 1000
                        });
                        var currentPage = jQuery('#carapplyGird').jqGrid('getGridParam', 'page');
                        var pageSize = jQuery('#carapplyGird').jqGrid('getGridParam', 'rowNum');
                        var totalCount = jQuery('#carapplyGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                        var totalPages = Math.ceil(totalCount / pageSize);
                        jQuery("#carapplyGird").jqGrid('setGridParam', {
                            page : currentPage > totalPages ? totalPages : currentPage
                        }).trigger("reloadGrid");
                    }, function(error) {
                        alert(error);
                    });
                };
                carapplySubmitForm = function(formId, type) {
                    var url = "carapply/insert";
                    if (type == 'u') {
                        url = "carapply/update";
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var formData = form.getValues();
                        lang.mixin(formData,{
                            userid : userid,
                            proposer : username
                        });
                        var json = JSON.stringify(formData);
                        syncDB(url, json);
                        form.hide();
                        topic.publish("carapply/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                carapplyDelete = function() {
                    var ids = jQuery("#carapplyGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var carapply = {};
                            carapply.id = '';
                            array.forEach(ids, function(entry, i) {
                                carapply.id += entry + ",";
                            });
                            syncDB("carapply/delete", JSON.stringify(carapply));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                carapplyQuery = function(formId) {
                    var carapply = dijit.byId(formId).getValues();
                    jQuery("#carapplyGird").jqGrid('setGridParam', {
                        postData : carapply
                    }).trigger('reloadGrid');
                };
            });
</script>
<div data-dojo-type="dijit/Dialog" id="carapplyIFD" title="新增">
  <div class="dijitDialogPaneContentArea">
    <table>
      <tr>
        <td><label for="carid">车牌号: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="carid"></td>
      </tr>
      <tr>
        <td><label for="department">用车单位: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="department"></td>
      </tr>
      <tr>
        <td><label for="userfullname">用车人: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="userfullname"></td>
      </tr>
      <tr>
        <td><label for="reason">用车原因: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="reason"></td>
      </tr>
      <tr>
        <td><label for="starttime">开始时间: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox"  data-dojo-props="required: true, trim:true" type="text" name="starttime" class="Wdate" id="carapply_starttime"
          onClick="WdatePicker({maxDate:'#F{$dp.$D(\'carapply_endtime\')}'})"></td>
      </tr>
      <tr>
        <td><label for="endtime">结束时间: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox"  data-dojo-props="trim:true" type="text" name="endtime" class="Wdate" id="carapply_endtime"
          onClick="WdatePicker({minDate:'#F{$dp.$D(\'carapply_starttime\')}'})"></td>
      </tr>
      <tr>
        <td><label for="mileage">里程: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="trim:true,regExp: '^(-?\\d+)(\\.\\d+)?$', invalidMessage:'请输入数字'" type="text" name="mileage"></td>
      </tr>
      <tr>
        <td><label for="oiluse">油耗: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="trim:true,regExp: '^(-?\\d+)(\\.\\d+)?$', invalidMessage:'请输入数字'" type="text" name="oiluse"></td>
      </tr>
      <tr>
        <td><label for="notes">备注: </label></td>
        <td><input data-dojo-type="dijit/form/SimpleTextarea" data-dojo-props="trim:true" type="text" name="notes"></td>
      </tr>
      <tr  style="display: none;">
        <td><label for="approvalstatus" >审批状态： </label></td>
        <td><select data-dojo-type="dijit/form/Select" name="approvalstatus" data-dojo-props="required: true, trim:true, readOnly: true" style="width: 182px;">
            <option value="1">未审核</option>
        </select></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="carapplySubmitForm('carapplyIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="carapplyRestForm('carapplyIFD');">重置</button>
  </div>
</div>
<div data-dojo-type="dijit/Dialog" id="carapplyUFD" title="更新">
  <div class="dijitDialogPaneContentArea">
    <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, readOnly: true" type="text" name="applyid" style="display: none;">
    <table>
      <tr>
        <td><label for="carid">车牌号: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="carid"></td>
      </tr>
      <tr>
        <td><label for="department">用车单位: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="department"></td>
      </tr>
      <tr>
        <td><label for="userfullname">用车人: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="userfullname"></td>
      </tr>
      <tr>
        <td><label for="reason">用车原因: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="reason"></td>
      </tr>
      <tr>
        <td><label for="starttime">开始时间: </label></td>
        <td><input  data-dojo-type="dijit/form/ValidationTextBox" style="width: 180px;" name="starttime" class="Wdate" data-dojo-props="required: true" id="carapply_starttimeu" onClick="WdatePicker({maxDate:'#F{$dp.$D(\'carapply_endtimeu\')}'})"></td>
      </tr>
      <tr>
        <td><label for="endtime">结束时间: </label></td>
        <td><input  data-dojo-type="dijit/form/ValidationTextBox" style="width: 180px;" name="endtime" class="Wdate" id="carapply_endtimeu" onClick="WdatePicker({minDate:'#F{$dp.$D(\'carapply_starttimeu\')}'})"></td>
      </tr>
      <tr>
        <td><label for="mileage">里程: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="trim:true,regExp: '^(-?\\d+)(\\.\\d+)?$', invalidMessage:'请输入数字'" type="text" name="mileage"></td>
      </tr>
      <tr>
        <td><label for="oiluse">油耗: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="trim:true,regExp: '^(-?\\d+)(\\.\\d+)?$', invalidMessage:'请输入数字'" type="text" name="oiluse"></td>
      </tr>
      <tr>
        <td><label for="notes">备注: </label></td>
        <td><input data-dojo-type="dijit/form/SimpleTextarea"  type="text" name="notes"></td>
      </tr>

    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="carapplySubmitForm('carapplyUFD','u')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="carapplyRestForm('carapplyUFD');">重置</button>
  </div>
</div>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="queryCarApplyForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
      <table cellspacing="10">
        <tr>
          <td><label for="carid">车牌号: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="carid"></td>
          <td><label for="approvalstatus">审批状态： </label></td>
          <td><select data-dojo-type="dijit/form/Select" name="approvalstatus" style="width: 100px;">
              <option value="0">全部</option>
              <option value="1">未审核</option>
              <option value="2">审核通过</option>
              <option value="3">审核未通过</option>
          </select></td>
          <td></td>
          <!--  
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="approvalstatus"></td>
          -->
        </tr>
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="carapplyInsertBtn" data-dojo-type="dijit/form/Button" type="submit" label="新增">
      <script type="dojo/method" event="onClick" args="evt">
        dijit.byId('carapplyIFD').show();
      </script>
    </button>
    <button id="carapplyDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除" onclick="carapplyDelete();"></button>
    <button id="carapplyUpdateBtn" data-dojo-type="dijit/form/Button" type="button" label="更新">
      <script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#carapplyGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var carapply = jQuery("#carapplyGird").jqGrid('getRowData', ids[0]);
          dijit.byId('carapplyUFD').setValues(carapply);
          dijit.byId('carapplyUFD').show();
        }
      </script>
    </button>
    <button id="carapplyQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询" onclick="carapplyQuery('queryCarApplyForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="carapplyGird"></table>
  <div id="carapplyToolbar"></div>
</div>
<script type="text/javascript">
    jQuery("#carapplyGird").jqGrid({
        url : 'carapply/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 'auto',
        colNames : [ '申请ID', '车牌号', '用车单位', '用车人', '申请时间', '用车原因', '开始时间', '结束时间', '申请人用户ID', '申请人', '里程',
                '油耗', '备注', '审批状态' ],
        colModel : [ {
            name : 'applyid',
            index : 'applyid',
            width : 60,
            key : true,
            hidden : true,
            align : 'center'
        }, {
            name : 'carid',
            width : 60,
            index : 'carid',
            align : 'center'
        }, {
            width : 90,
            name : 'department',
            index : 'department',
            align : 'center'
        }, {
            width : 80,
            name : 'userfullname',
            index : 'userfullname',
            align : 'center'
        }, {
            name : 'applydate',
            width : 140,
            index : 'applydate',
            align : 'center'
        }, {
            name : 'reason',
            index : 'reason',
            align : 'center',
            width : 100
        }, {
            name : 'starttime',
            width : 100,
            index : 'starttime',
            align : 'center'
        }, {
            name : 'endtime',
            width : 100,
            index : 'endtime',
            align : 'center'
        }, {
            name : 'userid',
            index : 'userid',
            hidden : true
        }, {
            name : 'proposer',
            width : 80,
            index : 'proposer',
            align : 'center'
        }, {
            name : 'mileage',
            index : 'mileage',
            hidden : true
        }, {
            name : 'oiluse',
            index : 'oiluse',
            hidden : true
        }, {
            name : 'notes',
            index : 'notes',
            hidden : true
        }, {
            //：1未审批、2已审批
            width : 80,
            name : 'approvalstatus',
            index : 'approvalstatus',
            align : 'center'
        } ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#carapplyToolbar',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },
        gridComplete : function() {
            var ids = jQuery("#carapplyGird").jqGrid('getDataIDs');
            for ( var i = 0; i < ids.length; i++) {
                var cl = ids[i];
                var carcheck = jQuery("#carapplyGird").jqGrid('getRowData', cl);
                var status = carcheck['approvalstatus'] == '1' ? '未审核'
                        : (carcheck['approvalstatus'] == '2' ? '审核通过' : '审核未通过');
                jQuery("#carapplyGird").jqGrid('setRowData', cl, {
                    approvalstatus : status
                }); 
            }
        },
        multiselect : true,
        shrinkToFit : false,
        caption : "车辆申请表列表"
    });
    jQuery("#carapplyGird").jqGrid('navGrid', '#carapplyToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
